<template>
	<view class="app-container">
		<!-- 顶部导航栏 -->
		<navbar :title="currentPageTitle" @toggle-sidebar="toggleSidebar"></navbar>

		<!-- 侧边栏 -->
		<sidebar :show="showSidebar" :active="activeMenu" @change="changeMenu" @close="toggleSidebar(false)"></sidebar>

		<!-- 内容区 -->
		<view class="content-container" :class="{'sidebar-open': showSidebar}">
			<!-- 根据当前菜单显示不同内容 -->
			<home v-if="activeMenu === 'home'"></home>
			<student v-if="activeMenu === 'student'"></student>
			<tools v-if="activeMenu === 'tools'"></tools>
			<news v-if="activeMenu === 'news'"></news>
			<clock v-if="activeMenu === 'clock'"></clock>
		</view>

		<!-- 消息通知 -->
		<newsmodal v-if="showNotification" :visible="showNotification" content="请同学们整理好自己的作业，书本，马上要下雨啦，咱们到17:50下课"
			sender="李校长" @close="showNotification = false" />
	</view>
</template>

<script>
	import sidebar from '@/components/sidebar/sidebar.vue'
	import navbar from '@/components/navbar/navbar.vue'
	import home from '@/pages/home/home.vue'
	import student from '@/pages/student/student.vue'
	import tools from '@/pages/tools/tools.vue'
	import news from '@/pages/news/news.vue'
	import clock from '@/pages/clock/clock.vue'

	export default {
		components: {
			sidebar,
			navbar,
			home,
			student,
			tools,
			news,
			clock
		},
		data() {
			return {
				showNotification: false,
				showSidebar: false,
				activeMenu: 'home', // 默认首页
				menuTitles: {
					home: '首页',
					student: '学生功能',
					tools: '工具游戏',
					news: '咨讯新闻',
					clock: '时钟'
				}
			}
		},
		computed: {
			currentPageTitle() {
				return this.menuTitles[this.activeMenu] || '大辛辅导班'
			}
		},
		onLoad() {
			// 从缓存中读取上次访问的页面
			this.loadLastActiveMenu();
		},
		methods: {
			toggleSidebar(show) {
				this.showSidebar = show !== undefined ? show : !this.showSidebar
			},
			changeMenu(menu) {
				this.activeMenu = menu;
				// 保存到缓存
				this.saveActiveMenu(menu);
				this.toggleSidebar(false);
			},
			// 保存当前页面到缓存
			saveActiveMenu(menu) {
				uni.setStorage({
					key: 'lastActiveMenu',
					data: menu,
					success: () => {
						console.log('当前页面已保存到缓存:', menu);
					}
				});
			},
			// 从缓存加载上次访问的页面
			loadLastActiveMenu() {
				uni.getStorage({
					key: 'lastActiveMenu',
					success: (res) => {
						if (res.data && this.menuTitles[res.data]) {
							this.activeMenu = res.data;
							console.log('从缓存加载上次访问的页面:', res.data);
						}
					}
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.app-container {
		display: flex;
		flex-direction: column;
		height: 100vh;
		position: relative;

		.content-container {
			flex: 1;
			overflow: auto;
			transition: all 0.3s ease;

			&.sidebar-open {
				transform: translateX(20%);
				filter: brightness(0.8);
			}
		}
	}
</style>